import 'dart:async';
import 'package:book_manager_app/utils/common/user/account_manager.dart';
import 'package:common_utils/common_utils.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import '../routes/app_pages.dart';
import '../theme/colors.dart';
import '../utils/common/app_manager.dart';


class RootMiddleWare extends  GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {

    final token = AppManager.userToken;
    final history = AccountManager.getLoginHistory();

    if (ObjectUtil.isEmpty(token) && ObjectUtil.isEmpty(history)) {
      debugPrint("AppManager.isLogin = false, RootMiddleWare 选择了登录界面");
      return const RouteSettings(name: Routes.LOGIN);
    }else {
      // if (AppManager.activeAdvert != null) {
      //   // debugPrint("AppManager.activeAdvert != null RootMiddleWare 选择了广告界面");
      //   // return const RouteSettings(name: "/ActiveAdvertPage");
      //   return null;
      // }else {
      //   // 不需要人脸认证了
      //   if (AppManager.getAuthState() != true && AppManager.isFirstInstall != true) {
      //     debugPrint("AppManager.getAuthState != true 需要进行生物识别认证！");
      //     debugPrint("RootMiddleWare 选择了认证界面");
      //     return const RouteSettings(name: "/FaceAuthPage");
      //   }else {
      //     debugPrint("AppManager.auth = true 不需要进行生物识别认证");
      //     // 正常跳转，不做处理
      //     debugPrint("RootMiddleWare 不做中间拦截处理，route = $route");
      //     // debugPrint("AppManager.userAccount == null, RootMiddleWare 选择了首页");
      //     // return const RouteSettings(name: AppRouter.index);
      //     return null;
      //   }
      //   return null;
      // }
    }
    return null;
  }
}



class ActiveAdvertPage extends StatefulWidget {
  const ActiveAdvertPage({super.key});

  @override
  State<ActiveAdvertPage> createState() => _ActiveAdvertPageState();
}

class _ActiveAdvertPageState extends State<ActiveAdvertPage> {
  late Timer _timer;
  int _countdownTime = 0;
  bool _showActiveAdvert = false;

  @override
  void initState() {
    super.initState();
    debugPrint("延长启动，开始展示AppManager.launchImage!");
    // if (AppManager.activeAdvert != null) {
    //   _showActiveAdvert = AppManager.activeAdvert != null ? true : false ;
    //   _countdownTime = int.parse(AppManager.activeAdvert!.twinkleDate!);
    //   _timer = Timer.periodic(
    //       const Duration(seconds: 1), (Timer timer) => setState(() {
    //     if (_countdownTime == 1) {
    //       _showActiveAdvert = false;
    //       _timer.cancel();
    //     } else {
    //       _countdownTime = _countdownTime - 1;
    //     }
    //   })
    //   );
    // }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Colors.white,
        child: Stack(
          fit: StackFit.expand,
          children: [
            // 模拟启动图为背景图（缓解网络图片加载中时的白屏）
            const Image(image: AssetImage("images/launch_image.png"), fit: BoxFit.cover),
            // 多张图片需要切换 未完成
            Image(image: AppManager.launchImage!, fit: BoxFit.cover),
            Align(
              alignment: Alignment.topRight,
              child: Container(
                width: 100, height: 36,
                margin: const EdgeInsets.only(top: 88, right: 20),
                decoration: const BoxDecoration(
                  color: BGColor_grey_201,
                  borderRadius: BorderRadius.all(Radius.circular(5.0)),
                ),
                child: TextButton(
                    onPressed: (){
                      _showActiveAdvert = false;
                      _timer.cancel();
                      setState(() { });
                    },
                    child: Text('${_countdownTime}s跳过广告', style: const TextStyle(fontSize: 14.0)
                    )
                ),
              ),
            ),
          ],
        )
    );
  }
}
